package com.zc.commons.http.vo;

import com.github.pagehelper.Page;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data;

import java.util.List;

/**
 * 分页返回实体
 *
 * @author zcj
 * @version 1.0.0
 * @date 2022/6/9 16:25
 */
@Data
@Schema(name = "分页返回实体", description = "分页返回实体")
public class PageResult<T> {

    public PageResult() {
    }

    public PageResult(Integer pageNo, Integer pageSize, Integer total) {
        this.pageNo = pageNo;
        this.pageSize = pageSize;
        this.total = total;
    }

    @Schema(description = "页号码")
    private Integer pageNo;

    @Schema(description = "页条数")
    private Integer pageSize;

    @Schema(description = "页总数")
    private Integer pages;

    @Schema(description = "总条数")
    private Integer total;

    @Schema(description = "数据列表")
    private List<T> list;

    /**
     * 构造返回实体
     *
     * @param pageNo
     * @param pageSize
     * @param total
     * @param list
     * @param <E>
     * @return
     */
    public static <E> PageResult<E> bulid(int pageNo, int pageSize, int total, List<E> list) {
        PageResult<E> result = new PageResult<>(pageNo, pageSize, total);
        result.setPages(getPages(pageSize, total));
        result.setList(list);
        return result;
    }

    /**
     * 根据pageHelper构造返回实体
     *
     * @param page
     * @param list
     * @param <E>
     * @return
     */
    public static <E> PageResult<E> bulid(Page page, List<E> list) {
        return bulid(page.getPageNum(), page.getPageSize(), (int) page.getTotal(), list);
    }

    /**
     * 获取总页数
     *
     * @param pageSize
     * @param total
     * @return
     */
    public static final int getPages(int pageSize, int total) {
        int a = total % pageSize;
        int b = total / pageSize;
        return a > 0 ? b + 1 : b;
    }


}
